package cn.genmer.test.security.thread.threadcoreknowledge.createthreads.callable;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.*;

/**
 * @program: concurrency_practice
 * @description:
 * @author: Genmer
 * @create: 2020-10-11 20:15
 **/
public class CallableStyle  {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        //创建一个callable, 3秒后返回一个字符串
        Callable myCallable = new Callable() {
            @Override
            public Object call() throws Exception {

                Thread.sleep(3000);
                System.out.println("call方法执行l");
                return "call方法的返回值";
            }
        };

        System.out.println("提交任务之前 "+getStringDate());
    Future future = executorService.submit(myCallable);
    System.out.println("提交任务之后"+getStringDate());
    Thread.sleep(4000);
        System.out.println("4秒后获取结果 "+getStringDate());
    System.out.println("获取返回值: "+future.get());
    System.out.println("获取到结果之后 "+getStringDate());
    }

    public static String getStringDate(){
        Date currentTime = new Date();
        SimpleDateFormat format = new SimpleDateFormat("HHH:mm:ss");
        String dateString =format.format(currentTime);
        return dateString;
    }
}
